package com.beanu.arad.base.my_base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.beanu.arad.R;
import com.beanu.arad.base.BaseModel;
import com.beanu.arad.base.BasePresenter;
import com.beanu.arad.base.ToolBarFragment;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnItemLongClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnNetWorkErrorListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.util.List;

/**
 * Created by Administrator on 2018/4/10.
 */
public abstract class MyRefreshBaseFragment<P extends BasePresenter, M extends BaseModel> extends ToolBarFragment<P, M> {
    public static final int PAGE_SIZE = 20;//默认的每页加载的数据数量

    protected LRecyclerView lRecyclerView;
    protected LRecyclerViewAdapter lAdapter;

    protected ProgressLayout progressLayout;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lAdapter = new LRecyclerViewAdapter(initAdapter());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(getInflate(), container, false);
        initRecyclerView(v);
        reReq();
        return v;
    }

    protected abstract int getInflate();

    protected void initRecyclerView(View v) {
        lRecyclerView = getRecyclerView(v);
        progressLayout =findProgressLayout(v);
        if (null != lRecyclerView) {

            lRecyclerView.setRefreshHeader(new WineRefreshHeader(getActivity()));

            lRecyclerView.setAdapter(lAdapter);
            lRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

//            setLoadMoreEnabled(false);//默认不开启加载更多功能
            //设置底部加载文字提示
            lRecyclerView.setFooterViewHint("努力加载中", "加载完成", "加载失败,点击重试");

            lRecyclerView.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh() {
                    reqFirst();
                }
            });
            lRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore() {
                    reqMore();
                }
            });
            lRecyclerView.setOnNetWorkErrorListener(new OnNetWorkErrorListener() {
                @Override
                public void reload() {
                    reReq();
                }
            });
        }

        if (null != lAdapter) {
            //设置点击事件
            lAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View v, int position) {
                    MyRefreshBaseFragment.this.onItemClick(v, position);
                }
            });
            lAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
                @Override
                public void onItemLongClick(View v, int position) {
                    MyRefreshBaseFragment.this.onItemLongClick(v, position);
                }
            });
        }
    }

    protected abstract LRecyclerView getRecyclerView(View view);
    protected abstract ProgressLayout findProgressLayout(View view);

    protected void setLoadMoreEnabled(boolean enabled) {//是否开启加载更多
        if (null != lRecyclerView) {
            lRecyclerView.setLoadMoreEnabled(enabled);
        }
    }

    protected void setPullRefreshEnabled(boolean enabled) {//是否开启下拉刷新
        if (null != lRecyclerView) {
            lRecyclerView.setPullRefreshEnabled(enabled);
        }
    }

    protected void refreshComplete() {//当前页面数据加载完成
        if (null != lRecyclerView) {
            lRecyclerView.refreshComplete(PAGE_SIZE);
        }
    }

    protected void refreshNoMoreData() {//所有数据都加载完成了
        if (null != lRecyclerView) {
            lRecyclerView.setNoMore(true);
        }
    }

    protected void showContent(List<Integer> skipIds) {//显示页面正常的布局
        if (null != progressLayout) {
            if (isDataEmpty(skipIds)) {
                progressLayout.showContent();
            } else {
                progressLayout.showContent(skipIds);
            }
        }
    }

    protected void showEmptyView(List<Integer> skipIds) {//显示空数据页面
        if (null != progressLayout) {
            showEmptyView(0, getString(R.string.cart_null), getString(R.string.cart_null), skipIds);
        }
    }
    protected void showEmptyView(int icon, String title, String description, List<Integer> skipIds) {
        if (icon == 0) {//没有传图标的资源id
            icon = R.drawable.yewubaobiao;//默认空白图标
        }
        if (TextUtils.isEmpty(title)) {
            title = getString(0);
        }
        if (TextUtils.isEmpty(description)) {
            title = getString(0);
        }

        if (isDataEmpty(skipIds)) {
            progressLayout.showEmpty(icon, title, "");
        } else {
            progressLayout.showEmpty(icon, title, "", skipIds);
        }
    }

    protected void showError(View.OnClickListener onClickListener) {//显示错误页面
        if (null != progressLayout) {
            showError(0, getString(com.beanu.arad.R.string.default_error_title), getString(com.beanu.arad.R.string.default_error_detail)
                    , getString(com.beanu.arad.R.string.default_error_btn_txt), onClickListener);
        }
    }

    protected void showError(int icon, String title, String description, String btnTxt, View.OnClickListener onClickListener) {//显示错误页面
        if (icon == 0) {
            icon = com.beanu.arad.R.mipmap.no_content;//默认错误图标
        }
        if (TextUtils.isEmpty(title)) {
            title = getString(com.beanu.arad.R.string.default_error_title);
        }
        if (TextUtils.isEmpty(description)) {
            title = getString(com.beanu.arad.R.string.default_error_detail);
        }
        if (TextUtils.isEmpty(btnTxt)) {
            title = getString(com.beanu.arad.R.string.default_error_btn_txt);
        }

        progressLayout.showError(icon, title, description, btnTxt, onClickListener);
    }

    protected void onItemClick(View v, int pos) {

    }

    protected void onItemLongClick(View v, int pos) {

    }

    protected abstract RecyclerView.Adapter initAdapter();

    protected abstract void reqFirst();

    protected abstract void reqMore();

    protected abstract void reReq();
}
